package cn.rtplay.esp32camcar.toolUtils;

import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONReader;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Map;

/**
 * The type Conf util.
 */
public class ConfUtil {
    private static final ConfUtil instance = new ConfUtil();
    private final HashMap<String, String> color = new HashMap<>();
    private final HashMap<String, String> brand = new HashMap<>();
    private String modelObject;
    private final String TAG = "ConfUtil";

    private ConfUtil() {
    }

    /**
     * Gets instance.
     *
     * @return the instance
     */
    public static ConfUtil getInstance() {
        return instance;
    }

    /**
     * Analyse conf file.
     *
     * @param context the context
     */
    public void AnalyseConfFile(Context context) {
        LogUtil.e(TAG, "AnalyseConfFile");
        String confName = AppVersions.getInstance().getLocalConfName(context);
        String confPath = AppVersions.getInstance().getFilePath(context, "conf");
        if (confName.isEmpty()) {
            color.put("red", "#FF0000");
            color.put("yel", "#FFFF00");
            color.put("blo", "#000000");
            color.put("blu", "#0000FF");
            color.put("gre", "#00FF00");
            color.put("whi", "#FFFFFF");
            brand.put("aod", "aodi");
            brand.put("ben", "benz");
            brand.put("bmw", "bmw");
            brand.put("bra", "brabus");
            brand.put("bug", "bugatti");
            brand.put("fer", "ferrari");
            brand.put("for", "ford");
            brand.put("hon", "honda");
            brand.put("jie", "jiebao");
            brand.put("lam", "lamborghini");
            brand.put("mas", "mashaladi");
            brand.put("nis", "nissan");
            brand.put("por", "porsche");
            brand.put("rol", "rolls_royce");
            brand.put("tes", "tesila");
            brand.put("xia", "xiandai");
            brand.put("lyk", "lyken");
            modelObject = "{\"fer\":{\"g\":\"559GTB\",\"f\":\"F430\"},\"bmw\":{\"i\":\"i8\"},\"lyk\":{\"l\":\"Fenyr2019\"}}";
        } else {
            String confFullName = confPath + "/" + confName + ".conf";
            JSONReader reader = null;
            try {
                reader = new JSONReader(new FileReader(confFullName));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            if (reader != null) {
                reader.startObject();
                while (reader.hasNext()) {
                    String key = reader.readString();
                    switch (key) {
                        case "color":
                            reader.startObject();
                            while (reader.hasNext()) {
                                String objectKey = reader.readString();
                                String objectValue = reader.readObject().toString();
                                color.put(objectKey, objectValue);
                            }
                            reader.endObject();
                            break;
                        case "brand":
                            reader.startObject();
                            while (reader.hasNext()) {
                                String objectKey = reader.readString();
                                String objectValue = reader.readObject().toString();
                                brand.put(objectKey, objectValue);
                            }
                            reader.endObject();
                            break;
                        case "model":
                            modelObject = reader.readObject().toString();
                            break;
                        default:
                            reader.readObject();
                            break;
                    }
                }
                reader.endObject();
                reader.close();
            } else {
                LogUtil.e(TAG, "reader:null");
            }
        }
    }

    /**
     * Find color value string.
     *
     * @param colorKey the color key
     * @return the string
     */
    public String findColorValue(String colorKey) {
        return color.get(colorKey);
    }

    /**
     * Find brand name string.
     *
     * @param brandKey the brand key
     * @return the string
     */
    public String findBrandName(String brandKey) {
        return brand.get(brandKey);
    }

    /**
     * Find model name string.
     *
     * @param brandKey the brand key
     * @param modelKey the model key
     * @return the string
     */
    public String findModelName(String brandKey, String modelKey) {
        String retValue = null;
        JSONObject brandKeyObj = JSON.parseObject(modelObject);

        for (Map.Entry<String, Object> entry : brandKeyObj.entrySet()) {
            if (entry.getKey().equals(brandKey)) {
                JSONObject modelKeyObj = JSON.parseObject(entry.getValue().toString());
                for (Map.Entry<String, Object> entryMod : modelKeyObj.entrySet()) {
                    if (entryMod.getKey().equals(modelKey)) {
                        retValue = entryMod.getValue().toString();
                    }
                }
            }
        }
        return retValue;
    }
}
